package com.turing.post.salesmanage.impl;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.ejb.EJB;
import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;

import com.turing.post.account.util.MasterAccount;
import com.turing.post.account.util.MasterAccountDao;
import com.turing.post.capitalaccounts.util.BankAccount;
import com.turing.post.capitalaccounts.util.BankAccountDao;
import com.turing.post.cardmanagement.util.ActivateCard;
import com.turing.post.cardmanagement.util.ActivateCardDao;
import com.turing.post.cardtransactions.util.CardTransBusiness;
import com.turing.post.cardtransactions.util.RedempObject;
import com.turing.post.common.util.PCPSCommonDao;
import com.turing.post.common.util.SysOperLog;
import com.turing.post.common.util.SysOperLogDao;
import com.turing.post.organization.util.IssueCompany;
import com.turing.post.organization.util.IssueCompanyDao;
import com.turing.post.organization.util.IssueFeePolicy;
import com.turing.post.organization.util.IssueFeePolicyLimit;
import com.turing.post.organization.util.IssueFeePolicyLimitDao;
import com.turing.post.organization.util.SaleBranch;
import com.turing.post.organization.util.SaleBranchDao;
import com.turing.post.organization.util.VoucherBusinessBind;
import com.turing.post.organization.util.VoucherBusinessBindDao;
import com.turing.post.organization.util.VoucherStock;
import com.turing.post.organization.util.VoucherStockDao;
import com.turing.post.organization.util.VoucherStockDetail;
import com.turing.post.organization.util.VoucherStockDetailDao;
import com.turing.post.salesmanage.util.RedempOrder;
import com.turing.post.salesmanage.util.RedempOrderActionDetail;
import com.turing.post.salesmanage.util.RedempOrderActionDetailDao;
import com.turing.post.salesmanage.util.RedempOrderBusiness;
import com.turing.post.salesmanage.util.RedempOrderDao;
import com.turing.post.salesmanage.util.RedempOrderDetail;
import com.turing.post.salesmanage.util.RedempOrderDetailDao;
import com.turing.post.salesmanage.util.SalesManageApplicationException;
import com.turing.post.salesmanage.util.SalesManageRespCode;
import com.turing.post.util.PCPSApplicationException;
import com.turing.post.util.PCPSUtil;
import com.turing.post.util.PCPSWebInput;

/**
 * @author zxp
 * @Jan 29, 2013
 */
@Stateless(mappedName = "salesmanage.impl.RedempOrderBusinessImpl")
@Remote
@Local
public class RedempOrderBusinessImpl implements RedempOrderBusiness {

	@EJB(mappedName = "common.impl.SysOperLogDaoImpl")
	private SysOperLogDao sysOperLogDaoImpl;
	@EJB(mappedName = "common.impl.PCPSCommonDaoImpl")
	private PCPSCommonDao commonDaoImpl;
	@EJB(mappedName = "salesmanage.impl.RedempOrderDaoImpl")
	private RedempOrderDao redempOrderDaoImpl;
	@EJB(mappedName = "organization.impl.IssueCompanyDaoImpl")
	private IssueCompanyDao issueCompanyDaoImpl;
	@EJB(mappedName = "capitalaccounts.impl.BankAccountDaoImpl")
	private BankAccountDao bankAccountDaoImpl;
	@EJB(mappedName = "salesmanage.impl.RedempOrderActionDetailDaoImpl")
	private RedempOrderActionDetailDao redActionDelDaoImpl;
	@EJB(mappedName = "salesmanage.impl.RedempOrderDetailDaoImpl")
	private RedempOrderDetailDao redempOrderDetailDaoImpl;
	@EJB(mappedName = "cardtransactions.impl.CardTransBusinessImpl")
	private CardTransBusiness cardTransBusinessImpl;

	@EJB(mappedName = "cardmanagement.impl.ActivateCardDaoImpl")
	private ActivateCardDao activateCardDaoImpl;

	@EJB(mappedName = "account.impl.MasterAccountDaoImpl")
	private MasterAccountDao masterAccountDaoImpl;

	@EJB(mappedName = "organization.impl.VoucherBusinessBindDaoImpl")
	private VoucherBusinessBindDao voucherBusinessBindImpl;

	@EJB(mappedName = "organization.impl.VoucherStockDaoImpl")
	private VoucherStockDao voucherStockDaoImpl;

	@EJB(mappedName = "organization.impl.VoucherStockDetailDaoImpl")
	private VoucherStockDetailDao voucherStockDetailDaoImpl;

	@EJB(mappedName = "organization.impl.IssueFeePolicyLimitDaoImpl")
	private IssueFeePolicyLimitDao issueFeePolicyLimitDaoImpl;

	@EJB(mappedName = "organization.impl.SaleBranchDaoImpl")
	private SaleBranchDao saleBranchDaoImpl;

	/**
	 * 异常抛出方法
	 * 
	 * @param error
	 * @throws PCPSApplicationException
	 */
	private void errException(String error) throws PCPSApplicationException {
		throw new SalesManageApplicationException(
				SalesManageApplicationException.ERRCODE_CATEGORY_APPLICATION,
				SalesManageRespCode.DEPOSIT_EXCEPTIONCODE, error);
	}

	// 创建赎回单
	@Override
	public RedempOrder createRedempOrder(PCPSWebInput webInput,
			RedempOrder redempOrder, List<RedempOrderDetail> redOrderList)
			throws PCPSApplicationException {
		if (webInput.getOperBranchNo() == null || webInput.getOperId() == null) {
			errException("操作用户为空!");
		}
		redempOrder.setIssueCompanyNo(PCPSUtil.getIssueCompanyNo(webInput));
		redempOrder.setSaleBranchNo(webInput.getOperBranchNo());
		// 发卡商
		IssueCompany issueCompany = issueCompanyDaoImpl
				.getIssueCompanyByIssueCompanyNo(PCPSUtil
						.getIssueCompanyNo(webInput));
		redempOrder.setTranDate(issueCompany.getTranDate());// 帐务日期
		redempOrder.setRedempTime(commonDaoImpl.getDatabaseSysTime());// 赎回时间
		redempOrder.setOrderStatus(RedempOrder.OrderStatus_Confirm);// 终审通过
		SaleBranch sb = saleBranchDaoImpl.getSaleBranchBySaleBranchNo(webInput
				.getOperBranchNo());
		if (sb.getDayOff().equals(SaleBranch.In_DayOff)) {
			errException("销售机构“" + sb.getSaleBranchName() + "”已经日结!");
		}
		// 付款人银行账户信息
		if (redempOrder.getPaidAccountId() != null) {
			// 收款人---->发卡商银行检查
			List<BankAccount> baList = bankAccountDaoImpl
					.getBankAccountDropdown(webInput,
							BankAccount.BANKTYPE_ISSUECOMPANY);
			boolean tag = false;
			for (BankAccount iba : baList) {
				if (redempOrder.getPaidAccountId().compareTo(
						iba.getBankAccountId()) == 0) {
					tag = true;
				}
			}
			if (!tag) {
				errException("收款人银行账户不为发卡商银行账户!");
			}
		}

		if (redOrderList == null || redOrderList.size() < 1) {
			errException("赎回单明细为空!");
		}
		List<RedempOrderDetail> insertRodList = new ArrayList<RedempOrderDetail>();
		RedempOrderDetail rod = null;
		BigInteger allRedempAmount = BigInteger.ZERO;// 赎回金额
		BigInteger allFee = BigInteger.ZERO;// 手续费
		BigInteger allFactRedempAmount = BigInteger.ZERO;// 实际赎回金额
		for (int i = 0; i < redOrderList.size(); i++) {
			rod = getRedempOrderDetail(redOrderList.get(i), issueCompany,
					redempOrder.getOperId());
			allRedempAmount = allRedempAmount.add(rod.getRedempAmount());
			allFee = allFee.add(rod.getFee());
			allFactRedempAmount = allFactRedempAmount.add(rod
					.getFactRedempAmount());
			insertRodList.add(rod);
		}
		if (redempOrder.getTotalFactFee().compareTo(BigInteger.ZERO) < 0) {
			errException("实收手续费不可小于0!");
		}

		// 手续费上下限
		IssueFeePolicyLimit ifpl = issueFeePolicyLimitDaoImpl
				.getIssueFeePolicyLimitByCompNoAndTypeForEffective(redempOrder
						.getIssueCompanyNo(),
						IssueFeePolicy.IFPolicy_busiType_redemp);
		if (ifpl != null) {
			// 上限
			if (ifpl.getFeeUpperLimit().compareTo(BigInteger.ZERO) > 0) {
				if (allFee.compareTo(ifpl.getFeeUpperLimit()) > 0) {
					allFee = ifpl.getFeeUpperLimit();
				}
			}
			// 下限
			if (ifpl.getFeeLowerLimit().compareTo(BigInteger.ZERO) > 0) {
				if (allFee.compareTo(ifpl.getFeeLowerLimit()) < 0) {
					allFee = ifpl.getFeeLowerLimit();
				}
			}
		}

		// 更新_redOrder
		redempOrder.setTotalRedempAmount(allRedempAmount);
		redempOrder.setTotalFee(allFee);
		redempOrder.setTotalFactRedempAmount(allFactRedempAmount);

		redempOrder.setOperId(webInput.getOperId());
		BigInteger id = commonDaoImpl.getUniqueIntegerKey();
		// 主键
		redempOrder.setRedempOrderId(id);
		// 业务单号
		redempOrder.setRedempOrderSeqNo(commonDaoImpl.getOrderNumber(
				issueCompany.getTranDate(), redempOrder.getSaleBranchNo()));
		// 插入数据
		redempOrderDaoImpl.insert(redempOrder);
		// 插入状态迁移表
		insertRedempOrderActionDetail(redempOrder.getRedempOrderId(), webInput
				.getOperId(), RedempOrder.OrderStatus_Input,
				RedempOrder.OrderStatus_Confirm,
				RedempOrderActionDetail.RedOrdDetail_Type_Input, redempOrder
						.getRemark());
		for (RedempOrderDetail rods : insertRodList) {
			rods.setRedempOrderId(id);
			redempOrderDetailDaoImpl.insertRedempOrderDetail(rods);
		}
		// 赎回单确认(只验证,增加本单资金为待审核)
		cardTransBusinessImpl.ConfirmRedempOrder(id);
		// 插入日志
		sysOperLogDaoImpl.insertSysOperLogInfo(webInput,
				SysOperLog.operType_Add, "创建赎回单业务", "赎回单编号 " + id);
		return redempOrder;
	}

	/**
	 * 取消赎回单
	 * 
	 * @param webInput
	 * @param redempOrder
	 * @throws PCPSApplicationException
	 */
	public void cancelRedempOrder(PCPSWebInput webInput, RedempOrder redempOrder)
			throws PCPSApplicationException {
		if (webInput.getOperBranchNo() == null || webInput.getOperId() == null) {
			errException("操作用户为空!");
		}
		RedempOrder _redOrder = getValidateRedempOrder(redempOrder == null ? null
				: redempOrder.getRedempOrderId());
		// 判断状态
		if (!_redOrder.getOrderStatus().equals(RedempOrder.OrderStatus_Confirm)) {
			errException("赎回单只有在创建完成状态才可取消!");
		}
		// 录入人取消
		if (webInput.getOperId().compareTo(_redOrder.getOperId()) != 0) {
			errException("赎回单只有创建人才可取消!");
		}
		SaleBranch sb = saleBranchDaoImpl.getSaleBranchBySaleBranchNo(webInput
				.getOperBranchNo());
		if (sb.getDayOff().equals(SaleBranch.In_DayOff)) {
			errException("销售机构“" + sb.getSaleBranchName() + "”已经日结!");
		}
		// 赎回单
		String initStatus = _redOrder.getOrderStatus();
		_redOrder.setOrderStatus(RedempOrder.OrderStatus_CanRefuse);
		_redOrder.setCancelOperId(redempOrder.getCancelOperId());
		_redOrder.setCancelTime(commonDaoImpl.getDatabaseSysTime());
		_redOrder.setCancelOperId(webInput.getOperId());
		// 发卡商
		IssueCompany issueCompany = issueCompanyDaoImpl
				.getIssueCompanyByIssueCompanyNo(PCPSUtil
						.getIssueCompanyNo(webInput));
		_redOrder.setCancelDate(issueCompany.getTranDate());
		redempOrderDaoImpl.update(_redOrder);
		// 赎回单确认(只验证,增加本单资金为待审核)
		cardTransBusinessImpl.CancelConfirmRedempOrder(webInput, redempOrder
				.getRedempOrderId());
		// 插入状态迁移表
		insertRedempOrderActionDetail(_redOrder.getRedempOrderId(), webInput
				.getOperId(), initStatus, _redOrder.getOrderStatus(),
				RedempOrderActionDetail.RedOrdDetail_Type_Cancel, _redOrder
						.getRemark());
	}

	private RedempOrderDetail getRedempOrderDetail(RedempOrderDetail rod,
			IssueCompany issueCompany, BigInteger operId)
			throws PCPSApplicationException {
		if (rod == null) {
			errException("赎回单明细单对象为空!");
		}
		if (PCPSUtil.isEmpty(rod.getCardNo())) {
			errException("卡号为空!");
		}
		if (rod.getRedempAmount() == null
				|| rod.getRedempAmount().compareTo(BigInteger.ZERO) <= 0) {
			errException("赎回金额输入错误!");
		}
		// 验证卡信息
		ActivateCard ac = activateCardDaoImpl.getActivateCardByCardNo(rod
				.getCardNo());
		if (ac == null) {
			errException("卡" + rod.getCardNo() + "为无效卡!");
		}
		// 验证主账号
		MasterAccount ma = masterAccountDaoImpl.getMasterAccountByCardNo(rod
				.getCardNo());
		if (ma == null) {
			errException("未找到卡" + rod.getCardNo() + "主账号!");
		}
		// 赎回期限
		if (issueCompany.getRedempLimit().intValue() > 0) {
			if (!diffDate(issueCompany.getTranDate(), ac.getOpenTime(),
					issueCompany.getRedempLimit())) {
				errException("卡“" + ac.getCardNo() + "”必须使用“"
						+ issueCompany.getRedempLimit() + "”天后才可赎回!");
			}
		}
		// 前台已算
		RedempObject rdObj = new RedempObject();
		rdObj.cardNo = rod.getCardNo();
		rdObj.amount = rod.getRedempAmount();
		rdObj.operId = operId;
		rdObj.verifyPwd = false;
		RedempObject res = cardTransBusinessImpl.VerifyCardRedempOrder(rdObj);
		if (res.fee.compareTo(rod.getFee()) != 0) {
			throw new PCPSApplicationException("", "赎回手续费" + res.fee + "与传入手续费"
					+ rod.getFee() + "计算不符!");
		}

		if (rod.getFee() == null || rod.getFee().compareTo(BigInteger.ZERO) < 0) {
			errException("赎回单手续费为空或小于0!");
		}
		if (rod.getFactRedempAmount() == null
				|| rod.getFactRedempAmount().compareTo(BigInteger.ZERO) <= 0) {
			errException("实际赎回金额为空或小于0!");
		}

		// RedempEachCashLimit//现金赎回上限
		rod.setMasterAccNo(ma.getMasterAccNo());// 主帐号 int Not Null
		rod.setBeforeBalance(ac.getBalance());// 原余额 decimal(16) Not Null
		rod.setOwnerId(ac.getOwnerId());
		return rod;
	}

	private boolean diffDate(Date now, Date openTime, int day) {
		long startT = openTime.getTime();
		long endT = now.getTime();
		long ss = (startT - endT) / (1000); // 共计秒数
		int hh = (int) ss / 3600; // 共计小时数
		int dd = (int) hh / 24; // 共计天数
		if (dd > day) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 终审赎回单
	 * 
	 * @param webInput
	 * @param redempOrder
	 * @throws PCPSApplicationException
	 */
	public void secondAudtRedempOrder(PCPSWebInput webInput,
			RedempOrder redempOrder) throws PCPSApplicationException {
		if (webInput.getOperBranchNo() == null || webInput.getOperId() == null) {
			errException("操作用户为空!");
		}
		RedempOrder _redOrder = getValidateRedempOrder(redempOrder == null ? null
				: redempOrder.getRedempOrderId());
		// 判断状态
		if (!_redOrder.getOrderStatus().equals(RedempOrder.OrderStatus_Confirm)) {
			errException("赎回单只有在创建完成后可终审!");
		}
		// 赎回单
		String initStatus = _redOrder.getOrderStatus();
		_redOrder.setOrderStatus(RedempOrder.OrderStatus_SecPass);
		redempOrderDaoImpl.update(_redOrder);
		// 插入状态迁移表
		insertRedempOrderActionDetail(_redOrder.getRedempOrderId(), webInput
				.getOperId(), initStatus, _redOrder.getOrderStatus(),
				RedempOrderActionDetail.RedOrdDetail_Type_SecPass, _redOrder
						.getRemark());
	}

	/**
	 * 拒绝赎回单 扣减冻结资金----------
	 * 
	 * @param webInput
	 * @param redempOrder
	 * @throws PCPSApplicationException
	 */
	public void refuseRedempOrder(PCPSWebInput webInput, RedempOrder redempOrder)
			throws PCPSApplicationException {
		if (webInput.getOperBranchNo() == null || webInput.getOperId() == null) {
			errException("操作用户为空!");
		}
		RedempOrder _redOrder = getValidateRedempOrder(redempOrder == null ? null
				: redempOrder.getRedempOrderId());
		// 判断状态
		if (_redOrder.getOrderStatus().equals(RedempOrder.OrderStatus_Confirm)) {
			errException("赎回单只有创建状态下才可拒绝!");
		}
		// 赎回单确认取消或撤销(只验证,减少本单待审核资金)
		cardTransBusinessImpl.CancelConfirmRedempOrder(webInput, redempOrder
				.getRedempOrderId());
		// 赎回单
		String initStatus = _redOrder.getOrderStatus();
		_redOrder.setOrderStatus(RedempOrder.OrderStatus_Input);
		redempOrderDaoImpl.update(_redOrder);
		// 插入状态迁移表
		insertRedempOrderActionDetail(_redOrder.getRedempOrderId(), webInput
				.getOperId(), initStatus, _redOrder.getOrderStatus(),
				RedempOrderActionDetail.RedOrdDetail_Type_FirRefuse, _redOrder
						.getRemark());
	}

	/**
	 * 获取有效的赎回单
	 * 
	 * @param redempOrderId
	 * @return
	 * @throws PCPSApplicationException
	 */
	private RedempOrder getValidateRedempOrder(BigInteger redempOrderId)
			throws PCPSApplicationException {
		if (redempOrderId == null)
			errException("赎回单编号为空!");
		RedempOrder _redOrder = redempOrderDaoImpl
				.getRedempOrderForUpdate(redempOrderId);
		if (_redOrder == null)
			errException("赎回单ID无效!");
		return _redOrder;
	}

	/**
	 * 赎回单 付款
	 * 
	 * @param webInput
	 * @param redempOrder
	 * @throws PCPSApplicationException
	 */
	public void updateAccountRedempOrder(PCPSWebInput webInput,
			RedempOrder redempOrder) throws PCPSApplicationException {
		if (webInput.getOperBranchNo() == null || webInput.getOperId() == null)
			errException("操作用户为空!");
		RedempOrder _redOrder = getValidateRedempOrder(redempOrder == null ? null
				: redempOrder.getRedempOrderId());
		// 判断状态
		if (!_redOrder.getOrderStatus().equals(RedempOrder.OrderStatus_Debit))
			errException("赎回单只有已扣帐后才可付款!");
		// 凭证号
		if (PCPSUtil.isEmpty(redempOrder.getVoucherNo()))
			errException("赎回单付款时必须录入凭证号!");
		// // 凭证摘要
		// if (PCPSUtil.isEmpty(redempOrder.getVoucherRemark()))
		// errException("赎回单付款时必须录入凭证摘要!");
		// 赎回单
		String initStatus = _redOrder.getOrderStatus();
		_redOrder.setVoucherNo(redempOrder.getVoucherNo());
		_redOrder.setVoucherRemark(redempOrder.getVoucherRemark());
		_redOrder.setOrderStatus(RedempOrder.OrderStatus_Paid);
		_redOrder.setPaidAccountId(redempOrder.getPaidAccountId());
		redempOrderDaoImpl.update(_redOrder);
		// 插入状态迁移表
		insertRedempOrderActionDetail(_redOrder.getRedempOrderId(), webInput
				.getOperId(), initStatus, _redOrder.getOrderStatus(),
				RedempOrderActionDetail.RedOrdDetail_Type_PAID, redempOrder
						.getRemark());
	}

	/**
	 * 扣帐不成功时终止赎回单 扣减冻结资金----------
	 * 
	 * @param webInput
	 * @param redempOrder
	 * @throws PCPSApplicationException
	 */
	public void endRedempOrderByAccountFailed(PCPSWebInput webInput,
			RedempOrder redempOrder) throws PCPSApplicationException {
		if (webInput.getOperBranchNo() == null || webInput.getOperId() == null)
			errException("操作用户为空!");
		RedempOrder _redOrder = getValidateRedempOrder(redempOrder == null ? null
				: redempOrder.getRedempOrderId());
		// 判断状态
		if (!_redOrder.getOrderStatus().equals(RedempOrder.OrderStatus_SecPass))
			errException("赎回单只有在终审通过状态下才可因扣款不成功终止!");
		// 赎回单确认取消或撤销(只验证,减少本单待审核资金)
		cardTransBusinessImpl.CancelConfirmRedempOrder(webInput, redempOrder
				.getRedempOrderId());
		// 赎回单
		String initStatus = _redOrder.getOrderStatus();
		_redOrder.setOrderStatus(RedempOrder.OrderStatus_CanRefuse);
		redempOrderDaoImpl.update(_redOrder);
		// 插入状态迁移表
		insertRedempOrderActionDetail(_redOrder.getRedempOrderId(), webInput
				.getOperId(), initStatus, _redOrder.getOrderStatus(),
				RedempOrderActionDetail.RedOrdDetail_Type_ACCCANCEL,
				redempOrder.getRemark());
	}

	/**
	 * 状态迁移明细
	 * 
	 * @param redempOrderId
	 * @param operId
	 * @param initialStatus
	 * @param endStatus
	 * @param actionType
	 * @param remark
	 * @throws PCPSApplicationException
	 */
	private void insertRedempOrderActionDetail(BigInteger redempOrderId,
			BigInteger operId, String initialStatus, String endStatus,
			String actionType, String remark) throws PCPSApplicationException {
		// 记录状态迁移明细
		RedempOrderActionDetail road = new RedempOrderActionDetail();
		// 激活单号
		road.setRedempOrderId(redempOrderId);
		// 操作时间
		road.setActionTime(commonDaoImpl.getDatabaseSysTime());
		// 状态迁移表操作员号
		road.setOperId(operId);
		// 初始状态
		road.setInitialStatus(initialStatus);
		// 结束状态
		road.setEndStatus(endStatus);
		// ActionType
		road.setActionType(actionType);
		road.setRemark(remark);
		// 插入状态迁移明细
		redActionDelDaoImpl.insertRedempOrderActionDetail(road);
	}

	@Override
	public void updateRedempOrderVoucherNo(PCPSWebInput webInput,
			BigInteger redempOrderId, String voucherNo)
			throws PCPSApplicationException {
		String businessType = VoucherBusinessBind.BusinessType_02;
		List<VoucherBusinessBind> vbbList = voucherBusinessBindImpl
				.getVoucherBusinessBindByBusinessType(webInput, businessType,
						-1);
		if (vbbList == null || vbbList.size() < 1) {
			errException("未找到赎回业务对应凭证种类!");
		}
		// 凭证种类Id
		BigInteger voucherId = vbbList.get(0).getVoucherId();
		// 插入对象为null
		if (redempOrderId == null || webInput == null
				|| PCPSUtil.isEmpty(webInput.getOperBranchNo())) {
			errException("操作员或归属机构为空!");
		}
		RedempOrder so = redempOrderDaoImpl
				.getRedempOrderForUpdate(redempOrderId);
		if (so == null) {
			errException("赎回单编号错误!");
		} else {
			VoucherStock vs = new VoucherStock();
			vs.setVoucherId(voucherId);
			vs.setVoucherNo(voucherNo);
			VoucherStock serchVS = voucherStockDaoImpl.getVoucherStock(vs);
			if (serchVS == null
					|| !serchVS.getStatus().equals(VoucherStock.Status_0)) {
				String str = (serchVS.getStatus().equals("1")) ? "在途"
						: (serchVS.getStatus().equals("9")) ? "丢失、损坏等" : "";
				errException("凭证信息不存在或状态异常!" + str);
			} else {
				// 更新凭证库存
				serchVS.setStatus(VoucherStock.Status_2);
				voucherStockDaoImpl.updateVoucherStockByStatus(serchVS);
				// 插入库存出入库明细
				VoucherStockDetail voucherStockDetail = new VoucherStockDetail();
				voucherStockDetail.setIoOrderId(redempOrderId);
				voucherStockDetail.setIssueCompanyNo(PCPSUtil
						.getIssueCompanyNo(webInput));
				voucherStockDetail.setBranchNo(webInput.getOperBranchNo());
				IssueCompany ic = issueCompanyDaoImpl
						.getIssueCompanyByIssueCompanyNo(PCPSUtil
								.getIssueCompanyNo(webInput));
				voucherStockDetail.setTranDate(ic.getTranDate());
				voucherStockDetail.setTranTime(commonDaoImpl
						.getDatabaseSysTime());
				voucherStockDetail.setVoucherId(voucherId);
				voucherStockDetail.setStartVoucherNo(voucherNo);
				voucherStockDetail.setEndVoucherNo(voucherNo);
				voucherStockDetail.setCount(1);
				voucherStockDetail.setTranType(VoucherStockDetail.Status_4);
				voucherStockDetail.setOperId(webInput.getOperId());

				voucherStockDetailDaoImpl
						.insertVoucherStockDetail(voucherStockDetail);
			}
			so.setVoucherPrintId(voucherId);
			so.setVoucherPrintNo(voucherNo);
			redempOrderDaoImpl.update(so);
		}
	}
	/**
	 * 创建赎回单
	 * 
	 * @param webInput
	 * @param redempOrder
	 * @throws PCPSApplicationException
	 */
	// public BigInteger insertRedempOrder(PCPSWebInput webInput,
	// RedempOrder redempOrder) throws PCPSApplicationException {
	// if (webInput.getOperBranchNo() == null || webInput.getOperId() == null) {
	// errException("操作用户为空!");
	// }
	// redempOrder.setIssueCompanyNo(PCPSUtil.getIssueCompanyNo(webInput));
	// redempOrder.setSaleBranchNo(webInput.getOperBranchNo());
	// // 发卡商
	// IssueCompany issueCompany = issueCompanyDaoImpl
	// .getIssueCompanyByIssueCompanyNo(PCPSUtil
	// .getIssueCompanyNo(webInput));
	// redempOrder.setTranDate(issueCompany.getTranDate());// 帐务日期
	// redempOrder.setRedempTime(commonDaoImpl.getDatabaseSysTime());// 赎回时间
	// // 付款人银行账户信息
	// if (redempOrder.getPaidAccountId() != null) {
	// // 收款人---->发卡商银行检查
	// List<BankAccount> baList = bankAccountDaoImpl
	// .getBankAccountDropdown(webInput,
	// BankAccount.BANKTYPE_ISSUECOMPANY);
	// boolean tag = false;
	// for (BankAccount iba : baList) {
	// if (redempOrder.getPaidAccountId().compareTo(
	// iba.getBankAccountId()) == 0) {
	// tag = true;
	// }
	// }
	// if (!tag) {
	// errException("收款人银行账户不为发卡商银行账户!");
	// }
	// }
	// if (redempOrder.getPayeeAccountId() == null) {
	// errException("收款人银行账户为空!");
	// }
	// redempOrder.setOrderStatus(RedempOrder.OrderStatus_Input);
	// redempOrder.setOperId(webInput.getOperId());
	// BigInteger id = commonDaoImpl.getUniqueIntegerKey();
	// // 主键
	// redempOrder.setRedempOrderId(id);
	// redempOrder.setTotalRedempAmount(BigInteger.ZERO);// 总赎回金额
	// redempOrder.setTotalFee(BigInteger.ZERO);// 总手续费
	// redempOrder.setTotalFactRedempAmount(BigInteger.ZERO); // 总实际赎回金额
	// // 插入数据
	// redempOrderDaoImpl.insert(redempOrder);
	// return id;
	// }
	/**
	 * 修改赎回单
	 * 
	 * @param webInput
	 * @param redempOrder
	 * @throws PCPSApplicationException
	 */
	// public void updateRedempOrder(PCPSWebInput webInput, RedempOrder
	// redempOrder)
	// throws PCPSApplicationException {
	// if (webInput.getOperBranchNo() == null || webInput.getOperId() == null)
	// errException("操作用户为空!");
	// RedempOrder _redOrder = getValidateRedempOrder(redempOrder == null ? null
	// : redempOrder.getRedempOrderId());
	// // 判断状态
	// if (!_redOrder.getOrderStatus().equals(RedempOrder.OrderStatus_Input))
	// errException("赎回单只有在录入状态才可修改!");
	// List<RedempOrderDetail> rodList = redempOrderDetailDaoImpl
	// .getRedempOrderDetailListByRedempOrderId(redempOrder
	// .getRedempOrderId());
	// if (rodList.size() > 0)
	// errException("赎回单已录入明细禁止修改!");
	// SaleBranch sb = saleBranchDaoImpl.getSaleBranchBySaleBranchNo(webInput
	// .getOperBranchNo());
	// if (sb.getDayOff().equals(SaleBranch.In_DayOff)) {
	// errException("销售机构“" + sb.getSaleBranchName() + "”已经日结!");
	// }
	// // 付款人银行账户信息
	// if (redempOrder.getPaidAccountId() != null) {
	// // 收款人---->发卡商银行检查
	// List<BankAccount> baList = bankAccountDaoImpl
	// .getBankAccountDropdown(webInput,
	// BankAccount.BANKTYPE_ISSUECOMPANY);
	// boolean tag = false;
	// for (BankAccount iba : baList) {
	// if (redempOrder.getPaidAccountId().compareTo(
	// iba.getBankAccountId()) == 0) {
	// tag = true;
	// }
	// }
	// if (!tag) {
	// errException("收款人银行账户不为发卡商银行账户!");
	// }
	// }
	// // 修改数据
	// redempOrderDaoImpl.update(redempOrder);
	// }
	/**
	 * 确认赎回单 添加冻结资金==========
	 * 
	 * @param webInput
	 * @param redempOrder
	 * @throws PCPSApplicationException
	 */
	// public void confirmRedempOrder(PCPSWebInput webInput,
	// RedempOrder redempOrder) throws PCPSApplicationException {
	// if (webInput.getOperBranchNo() == null || webInput.getOperId() == null) {
	// errException("操作用户为空!");
	// }
	// RedempOrder _redOrder = getValidateRedempOrder(redempOrder == null ? null
	// : redempOrder.getRedempOrderId());
	// // 判断状态
	// if (!_redOrder.getOrderStatus().equals(RedempOrder.OrderStatus_Input)) {
	// errException("赎回单只有在录入状态才可确认!");
	// }
	// List<RedempOrderDetail> rodList = redempOrderDetailDaoImpl
	// .getRedempOrderDetailListByRedempOrderId(redempOrder
	// .getRedempOrderId());
	// if (rodList.size() < 1) {
	// errException("赎回单明细未录入!");
	// }
	// // 赎回单确认(只验证,增加本单资金为待审核)
	// cardTransBusinessImpl
	// .ConfirmRedempOrder(redempOrder.getRedempOrderId());
	// // 修改赎回单
	// String initStatus = _redOrder.getOrderStatus();
	// _redOrder.setOrderStatus(RedempOrder.OrderStatus_Confirm);
	// redempOrderDaoImpl.update(_redOrder);
	// // 插入状态迁移表
	// insertRedempOrderActionDetail(_redOrder.getRedempOrderId(), webInput
	// .getOperId(), initStatus, _redOrder.getOrderStatus(),
	// RedempOrderActionDetail.RedOrdDetail_Type_Input, _redOrder
	// .getRemark());
	// }
	// /**
	// * 初审赎回单
	// *
	// * @param webInput
	// * @param redempOrder
	// * @throws PCPSApplicationException
	// */
	// public void firstAudtRedempOrder(PCPSWebInput webInput,
	// RedempOrder redempOrder) throws PCPSApplicationException {
	// if (webInput.getOperBranchNo() == null || webInput.getOperId() == null) {
	// errException("操作用户为空!");
	// }
	// RedempOrder _redOrder = getValidateRedempOrder(redempOrder == null ? null
	// : redempOrder.getRedempOrderId());
	// // 判断状态
	// if (!_redOrder.getOrderStatus().equals(RedempOrder.OrderStatus_Confirm))
	// {
	// errException("赎回单只有在已确认状态才可初审!");
	// }
	// // 赎回单
	// String initStatus = _redOrder.getOrderStatus();
	// _redOrder.setOrderStatus(RedempOrder.OrderStatus_SecPass);
	// redempOrderDaoImpl.update(_redOrder);
	// // 插入状态迁移表
	// insertRedempOrderActionDetail(_redOrder.getRedempOrderId(), webInput
	// .getOperId(), initStatus, _redOrder.getOrderStatus(),
	// RedempOrderActionDetail.RedOrdDetail_Type_FirPass, _redOrder
	// .getRemark());
	// }
}